﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionTree
{
    static class TreeStatistics
    {
        /// <summary>
        ///   Retrieves a leaf distances array for given tree
        /// </summary>
        /// 
        /// <param name="node">Tree to be examined.</param>
        /// <returns>Leaf distances array for given tree</returns>
        /// 

        public static List<int> GetLeafDistances(DecisionTree tree)
        {
            List<int> distances = new List<int>();
            GetLeafDistances(tree.Root, distances);
            return distances;
        }

        /// <summary>
        ///   Retrieves a leaf distances array for given node
        /// </summary>
        /// 
        /// <param name="node">Node to be examined.</param>
        /// <returns>Leaf distances array for given node</returns>
        /// 

        private static void GetLeafDistances(DecisionNode node, List<int> distances)
        {
            if (node.IsLeaf)
                distances.Add(node.Level);
            else
            {
                for (int i = 0; i < node.Branches.Length; i++)
                {
                    GetLeafDistances(node.Branches[i], distances);
                }
            }
        }

        public static List<DecisionNode> GetLeafs(DecisionTree tree)
        {
            List<DecisionNode> leafs = new List<DecisionNode>();
            GetLeafs(tree.Root, leafs);
            return leafs;
        }

        private static void GetLeafs(DecisionNode node, List<DecisionNode> leafs)
        {
            if (node.IsLeaf)
                leafs.Add(node);
            else
            {
                for (int i = 0; i < node.Branches.Length; i++)
                {
                    GetLeafs(node.Branches[i], leafs);
                }
            }
        }
    }
}
